﻿#region Copyright
//       Copyright (c) 2009 - Antoine Jaussoin - http://www.jaussoin.com/blog/
// 
//        This program is free software: you can redistribute it and/or modify
//        it under the terms of the GNU General Public License as published by
//        the Free Software Foundation, either version 3 of the License, or
//        (at your option) any later version.
// 
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
// 
//        You should have received a copy of the GNU General Public License
//        along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Linq.Expressions;
using System.Windows.Forms;
using FT.Architecture.Entities;
using FT.Architecture.Utils.Reflection;

namespace FT.Architecture.Extensions
{
    /// <summary>
    /// Provide additional methods for <see cref="IEntity"/> and <see cref="IGenericEntity{TId}"/> objects
    /// </summary>
    public static class EntityExtensions
    {
        /// <summary>
        /// Helps building a solid binding, avoiding the usage of string
        /// </summary>
        /// <typeparam name="TControl"></typeparam>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TId"></typeparam>
        /// <param name="entity"></param>
        /// <param name="control"></param>
        /// <param name="controlProperty"></param>
        /// <param name="entityProperty"></param>
        /// <returns></returns>
        public static Binding DataBind<TControl, TEntity, TId>(
            this TEntity entity, 
            TControl control,
            Expression<Func<TControl, object>> controlProperty,
            Expression<Func<TEntity, object>> entityProperty) 
            where TEntity: IGenericEntity<TId>
            where TControl : IBindableComponent
        {
            string controlPropertyString = Reflection.GetCombinedProperty(controlProperty);
            string entityPropertyString = Reflection.GetCombinedProperty(entityProperty);

            //Check if there's already a binding, if it's the case, remove it
            Binding currentBinding = control.DataBindings[controlPropertyString];
            if (currentBinding != null)
                control.DataBindings.Remove(currentBinding);
            

            return control.DataBindings.Add(controlPropertyString, entity, entityPropertyString);
        }

        /// <summary>
        /// Helps building a solid binding, avoiding the usage of string
        /// </summary>
        /// <typeparam name="TControl"></typeparam>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="control"></param>
        /// <param name="controlProperty"></param>
        /// <param name="entityProperty"></param>
        /// <returns></returns>
        public static Binding DataBind<TControl, TEntity>(
            this TEntity entity,
            TControl control,
            Expression<Func<TControl, object>> controlProperty,
            Expression<Func<TEntity, object>> entityProperty)
            where TEntity : IEntity
            where TControl : IBindableComponent
        {
            return DataBind<TControl, TEntity, long>(entity, control, controlProperty, entityProperty);
        }
    }
}
